// index.js
import { get, post } from '../../utils/request'
import { APIS } from '../../utils/api'
import {timestampToTime, getHSTime} from '../../utils/util'
// 获取应用实例
const app = getApp()

Page({
  data: {
    currentIndex: 0,
    tabs: ['我的订单', '进行中', '待付款', '已完成'],
    // 1 进行中 2 待付款 3 已完成
    orderList: [],
    statusText: ['进行中', '等待付款', '已完成'],
    btnText: ['预计金额', '去付款', '已付款'],
    userInfo: {}
  },
  onLoad() {
    const userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    this.setData({
      userInfo
    });
  },
  onShow() {
    this.getOrderList(this.data.currentIndex);
  },
  getOrderList(status = 0) {
    get(APIS.order, {status, uid: this.data.userInfo.id}).then(res => {
      this.setData({
        orderList: res.data.map(x => {
          let end = x.end === '-' || !x.end ? Date.now() : Number(x.end);
          if (x.status === 1) {
            end = Date.now();
          }
          return {
            ...x,
            pstart: timestampToTime(Number(x.start), 1),
            pend: timestampToTime(Number(x.end), 1),
            total: getHSTime(end - Number(x.start))
          }
        })
      });
    })
  },
  handeToDetail(e) {
    const {index} = e.currentTarget.dataset;
    let order = this.data.orderList[index];
    wx.navigateTo({
      url: `/subPackagePay/pages/task?ball=${order.ltid}&order=${order.id}&type=${order.type}&dontShow=1`,
    })
  },
  handleChange(e) {
    const {
      index
    } = e.target.dataset;
    this.setData({
      currentIndex: index,
    });
    wx.pageScrollTo({
      scrollTop: 0,
      druation: 100
    })
    this.getOrderList(index)
  },
  onShareAppMessage() {
    return getApp().globalData.shareMessage
  }
})
